import java.awt.Color;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class gmm extends JFrame implements ActionListener{
	BufferedImage backbuffer, bg, player, mp;
	gmm gm;
	Timer t;
	BufferStrategy bs;
	
	int xcoord, ycoord;
	
	double msPerFrame=5;
	int frames=0;
	
	Color red = new Color(.9f, .1f, .1f, .7f);

	
	public gmm(){
		setTitle("DIY");
		gm = this;
		backbuffer = new BufferedImage(1280, 800, BufferedImage.TYPE_INT_RGB);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		try {
			bg = ImageIO.read(new File("Levels/15_bg.png"));
			player = ImageIO.read(new File("Levels/player_l.png"));
			mp = ImageIO.read(new File("Levels/moving_h.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(0);
		}
		setSize(1280, 800);
		setVisible(true);
		
		
		while(true){
			actionPerformed(null);
		}

		
	}
	public void update(Graphics g){
		paint(g);
	}
	public void paint(Graphics g){
		g.drawImage(backbuffer,0,0,null);
	}
	public void actionPerformed(ActionEvent e) {
		xcoord +=2;
		xcoord%=1280;
		ycoord%=800;
		
		paintBuffer();		
		repaint();
	}
	public void paintBuffer(){
		long ftime = System.currentTimeMillis();
		Graphics g = backbuffer.getGraphics();
		g.drawImage(bg, 0, 0, null);
		
		g.setColor(red);
		g.fillRect(xcoord, ycoord, 160, 40);
		g.setColor(Color.black);
		g.drawRect(xcoord, ycoord, 160, 40);
		
//		System.out.println(System.currentTimeMillis() - ftime + "ms per frame.");
		
		msPerFrame = (msPerFrame*frames+ ( System.currentTimeMillis() - ftime) )/(double)( ++frames);
		System.out.println("avg:"+(int)(1000/msPerFrame));
	}



	public static void main(String[]args) throws FontFormatException, IOException 
	{
		new gmm();
		
		
	}
}

